package com.ys.erp.gateway.config;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.ys.erp.framework.response.ResponseDTO;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * @author lwj
 * @date 2024/8/30
 */
public class GatewayErrHandler {

    public static Mono<Void> throwException(ServerWebExchange exchange, ResponseDTO<Object> responseCode) {
        return exchange.getResponse().writeWith(
                Mono.just(exchange.getResponse()
                        .bufferFactory()
                        .wrap(JSONObject.toJSONString(responseCode, JSONWriter.Feature.WriteMapNullValue).getBytes(StandardCharsets.UTF_8)))
        );
    }

}
